package com.jk.libsm.service.impl;

import com.jk.libsm.mapper.AdminMapper;
import com.jk.libsm.mapper.BookMapper;
import com.jk.libsm.pojo.Book;
import com.jk.libsm.pojo.Borrowandreturn;
import com.jk.libsm.pojo.Borrowhistory;
import com.jk.libsm.service.AdminService;
import com.jk.libsm.utils.DateUtil;
import com.jk.libsm.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lk
 * @date 2020/7/6 0006 23:16
 * @description:
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    BookMapper bookMapper;
    @Override
    public Result adminLogin(String id, String password) {
        String password1 = adminMapper.getUserPasswordById(id);
        Result result;
        if (password1 == null) {
            result = new Result("400", "用户不存在", "");
        } else if (!password.equals(password)) {
            result = new Result("400", "用户名或者密码错误", "");
        } else {
            String code = "200";
            String msg = "登陆成功";
            HashMap<String, String> data = new HashMap<>();
            data.put("token",id);
            result = new Result(code,msg,data);
        }
        return result;
    }

    @Override
    public Result adminAddBooks(Bookinfo bookinfo) {
        Result result;
        try {
            int BKNo = bookMapper.getBKNoByPosition(bookinfo.getPosition());
            Book book = new Book();
            book.setBName(bookinfo.getBook_name());
            book.setBKNo(BKNo);
            book.setBpublic(bookinfo.getBook_press());
            book.setBprice(bookinfo.getBook_price());
            book.setBauthor(bookinfo.getBook_author());
            book.setBintro(bookinfo.getBook_intro());
            book.setIsappoinment(1);
            book.setIsborrow(1);
            book.setBorrowCount(0);
            for (int i = 0; i < bookinfo.getBook_num(); i++) {
                bookMapper.saveBook(book);
            }
            result = new Result("200", "处理成功", "");
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result("400", "处理失败", "");
        }
        return result;
    }

    @Override
    public Result adminFindBooksBy(String token, int type, String info) {
        ArrayList<Books> list;
        int flag = 1;//可接书状态（1）
        if (type == 0) {//书名
            list = bookMapper.getAllBooksByName(info, flag);
        } else if (type == 1) {//种类
            list = bookMapper.getAllBooksByKind(info, flag);
        } else if (type == 2) {//出版社
            list = bookMapper.getAllBooksByPress(info, flag);
        } else if (type == 3) {//作者
            list = bookMapper.getAllBooksByAuthor(info, flag);
        } else if (type == 4) {//位置
            list = bookMapper.getAllBooksByPosition(info, flag);
        } else if (type == 5) { //所有
            list =adminMapper.getAllBooks();
        } else {//有误
            list = null;
        }
        Result result;
        if (list == null) {
            result = new Result("400", "处理失败", "");
        } else {
            ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
            for (Books books : list) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("book_id",books.getBNo());
                map.put("book_name",books.getBName());
                map.put("book_author",books.getBauthor());
                map.put("book_type",books.getBKName());
                map.put("book_press",books.getBpublic());
                map.put("position",books.getPosition());
                list1.add(map);
            }
            result = new Result("200", "处理成功", list1);
        }
        return result;
    }

    @Override
    public Result adminDelete(String token, int book_id) {
        Result result;
        try {
            adminMapper.deletebook(book_id);
            return result = new Result("200", "处理成功","");
        }catch (Exception e){
            e.printStackTrace();
            return  result = new Result("400", "处理失败","");
        }
    }

    @Override
    public Result adminFindAllUsers(String token) {
        Result result;
        try{
            ArrayList<Userinfo> list;
            list =adminMapper.getAlluser();
            ArrayList<Map<String,Object>> list1=new ArrayList<>();
            for(Userinfo userinfo:list){
                HashMap<String,Object> map=new HashMap<>();
                map.put("id",userinfo.getRNo());
                map.put("name",userinfo.getRName());
                map.put("phone_num",userinfo.getReaderPhonenumber());
                map.put("gender",userinfo.getRSex());
                map.put("adress",userinfo.getReaderAddress());
                map.put("state",userinfo.getReaderPermission());
                list1.add(map);
            }
            return  result = new Result("200", "处理成功",list1);
        }catch (Exception e){
            e.printStackTrace();
            return  result = new Result("400", "处理失败","");
        }
    }

    @Override
    public Result adminFindUserById(String token, String id) {
        Result result;
        try{
            ArrayList<Userinfo> list;
            list =adminMapper.getUserById(id);
            ArrayList<Map<String,Object>> list1=new ArrayList<>();
            for(Userinfo userinfo:list){
                HashMap<String,Object> map=new HashMap<>();
                map.put("id",userinfo.getRNo());
                map.put("name",userinfo.getRName());
                map.put("phone_num",userinfo.getReaderPhonenumber());
                map.put("gender",userinfo.getRSex());
                map.put("adress",userinfo.getReaderAddress());
                map.put("state",userinfo.getReaderPermission());
                list1.add(map);
            }
            return  result = new Result("200", "处理成功",list1);
        }catch (Exception e){
            e.printStackTrace();
            return  result = new Result("400", "处理失败","");
        }
    }

    @Override
    public Result adminUpdateUserAuth(String token, String id, int state) {
        Result result;
        try{
            adminMapper.setReaderPermission(state,id);
            return  result = new Result("200", "处理成功","");
        }catch (Exception e){
            e.printStackTrace();
            return  result = new Result("400", "处理失败","");
        }
    }

    @Override
    public Result adminBorrowBook(String token,String id,int book_id){
        Result result;
        try{
            String date= DateUtil.getNowDate();
            String datereturn=DateUtil.plusDay(30,date);
            adminMapper.deleteAppoinment(id,book_id);
            Borrowandreturn borrowandreturn = new Borrowandreturn();
            borrowandreturn.setRNo(id);
            borrowandreturn.setBNo(book_id);
            borrowandreturn.setBorrowdate(date);
            borrowandreturn.setShouldreturndate(datereturn);
            borrowandreturn.setIsreturnbook(0);
            borrowandreturn.setReborrow_state(1);
            adminMapper.insertBorrow(borrowandreturn);
            adminMapper.updateBookCount(book_id);
            return  result = new Result("200", "处理成功","");
        }catch (Exception e){
            e.printStackTrace();
            return  result = new Result("400", "处理失败","");
        }
    }

    @Override
    public Result adminRegist(String token,String id,String password,String name,String phone_num){
        Result result;
        try {
            adminMapper.insertAdmin(id, name, password, phone_num);
            return  result = new Result("200", "处理成功","");
        }catch (Exception e){
            e.printStackTrace();
            return  result = new Result("400", "处理失败","");
        }
        }

    @Override
    public Result adminReturnBook(String token,String id,int book_id){
        String borrowdate = bookMapper.getBorrowDate(id,book_id);
        String returndate = DateUtil.getNowDate();
        Borrowhistory borrowhistory = new Borrowhistory(book_id,id,borrowdate,returndate);
        Result result;
        try{
            bookMapper.saveBorrowHistory(borrowhistory);
            bookMapper.deleteReaderBorrow(id,book_id);
            bookMapper.updateBookBorrowState(1,1,book_id);
            result = new Result("200", "处理成功","");
        }catch (Exception e){
            e.printStackTrace();
            result = new Result("400", "处理失败","");
        }
        return  result;
    }
}
